﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.SessionState;
using System.Web.UI.MobileControls;
using System.Collections.Generic;

namespace YXShop.Web.filehandle
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class loginform : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string message = "";
            //检查浏览器是否打开Cookies
            if (HttpContext.Current.Request.Cookies["CheckCode"] == null)
            {
                message = "您的浏览器设置已被禁用 Cookies，您必须设置浏览器允许使用 Cookies 选项后才能使用本系统。";
                HttpContext.Current.Response.Write(message);
                HttpContext.Current.Response.End();
            }
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            YXShop.BLL.Member.MemberAccount memberBll = new YXShop.BLL.Member.MemberAccount();
            string name = ChangeHope.WebPage.PageRequest.GetFormString("txtLoginName") == "" ? "" : ChangeHope.WebPage.PageRequest.GetFormString("txtLoginName");
            string pass = ChangeHope.WebPage.PageRequest.GetFormString("txtPassword") == "" ? "" : ChangeHope.Common.DEncryptHelper.Encrypt(ChangeHope.WebPage.PageRequest.GetFormString("txtPassword"), 1); 
            string validate = ChangeHope.WebPage.PageRequest.GetFormString("txtCode") == null ? "" : ChangeHope.WebPage.PageRequest.GetFormString("txtCode");
            if(validate!=""&&validate!=null)
            {
                if (!HttpContext.Current.Request.Cookies["CheckCode"].Value.Equals(validate))
                {
                    HttpContext.Current.Response.Write("温馨提示：验证码错误");
                    HttpContext.Current.Response.End();
                }
            }
            if (name == "" || pass == "")
            {
                HttpContext.Current.Response.Write("温馨提示：用户名与密码不能为空");
                HttpContext.Current.Response.End();
            }
            YXShop.Model.Member.MemberAccount model = memberBll.GetModelByNameAndPassword(name,pass);
            if (model != null)
            {
                if (Convert.ToInt32(model.State) == 1)
                {
                    //未通过审核!
                    HttpContext.Current.Response.Write("温馨提示：该用户未通过审核");
                    HttpContext.Current.Response.End();
                }
                if (Convert.ToInt32(model.State) == 2)
                {
                    //被冻结
                    HttpContext.Current.Response.Write("温馨提示：该用户已被冻结");
                    HttpContext.Current.Response.End();
                }
                if (Convert.ToDateTime(model.PeriodOfValidity) < System.DateTime.Now)
                {
                    HttpContext.Current.Response.Write("温馨提示：该用户使用有效期已到，请与本站联系");
                    HttpContext.Current.Response.End();
                }
                    //执行登陆操作加积分
                    YXShop.BLL.Member.Integral integralBll = new YXShop.BLL.Member.Integral();
                    YXShop.Model.Member.Integral integral = new YXShop.Model.Member.Integral();
                    YXShop.Model.Member.MemberAccount merber = memberBll.GetModel(model.UID);
                    List<YXShop.Model.Member.Integral> inteList = integralBll.GetListByWhere("integralClass=3 and userId="+model.UID);
                    //后台设置积分数
                                     
                        if(merber!=null && Convert.ToDecimal(sp.LoginPoint) > 0)
                        {
                            merber.Points = Convert.ToDecimal(merber.Points) + Convert.ToDecimal(sp.LoginPoint);
                            merber.PassWord = "";
                            integral.Userid = merber.UID;
                            integral.OrderId = string.Empty;
                            integral.IntegralClass = 3;
                            integral.IntegralNum = Convert.ToDecimal(sp.LoginPoint);
                            integral.GainDate = Convert.ToDateTime(System.DateTime.Now);
                            integral.NoteDate = Convert.ToDateTime(System.DateTime.Now);
                            integral.NoteName = "系统";
                            integral.Remark = "系统登陆赠送";
                            integral.IntegralStatus = 0;
                            integral.Origin = "登陆";
                            //查看在规定时间内用户登陆是否已赠送积分（已登陆过的用户）
                            if (inteList.Count > 0 && inteList!=null)
                            {
                                TimeSpan oldtime = new TimeSpan(Convert.ToDateTime(inteList[0].GainDate).Ticks);
                                TimeSpan newtime = new TimeSpan(DateTime.Now.Ticks);
                                TimeSpan time = oldtime.Subtract(newtime).Duration();
                                int dateNum = Convert.ToInt32(time.Days.ToString());
                                if (dateNum > 1)
                                {
                                    memberBll.Update(merber);
                                    integralBll.Add(integral);
                                }
                            }
                            else
                            {
                                //用户第一次登陆
                                memberBll.Update(merber);
                                integralBll.Add(integral);
                            }

                            context.Session["MemberID"] = model.UID.ToString();
                            context.Session["MemberName"] = model.UserId;
                            context.Response.Write("true");
                            context.Response.End();
                                              
                        }
                    }

            else
            {
                HttpContext.Current.Response.Write("温馨提示：输入用户名密码错误");
                HttpContext.Current.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    
    }
}
